/*
 * Order Controller
 * @Author: chiangbt@geoportal.cn 
 * @Date: 2019-05-07 13:49:38 
 * @Last Modified by: chiangbt@geoportal.cn
 * @Last Modified time: 2019-05-15 15:10:20
 */
const mongoose = require('mongoose')
const Order = require("../models/order");
const Product = require("../models/product");

exports.order_get_all = (req, res, next) => {
    const limit = parseInt(req.query.limit) || 15
    const offset = parseInt(req.query.offset) || 0

    Order.count({}, function (err, counts) {
        Order.find().skip(offset).limit(limit)
            .select("_id product quantity")
            .populate("product", "name")
            .then(docs => {
                return res.status(200).json({
                    success: true,
                    count: counts,
                    orders: docs.map(doc => {
                        return {
                            id: doc._id,
                            quantity: doc.quantity,
                            product: {
                                id: doc.product._id,
                                name: doc.product.name
                            }
                        };
                    })
                });
            })
            .catch(err => {
                return res.status(500).json({
                    success: false,
                    error: err.message
                });
            });
    });
};

exports.create_order = (req, res, next) => {
    // 由于是关联对象,在保存order之前要查看其关联的product是否存在
    Product.findById(req.body.product)
        .then(product => {
            if (!product) {
                return res.status(404).json({
                    success: false,
                    message: "Product not found"
                });
            }
            const order = new Order({
                _id: mongoose.Types.ObjectId(),
                quantity: req.body.quantity,
                product: req.body.product
            });
            order.save()
                .then(result => {
                    return res.status(201).json({
                        success: true,
                        message: "Order创建成功",
                        order: {
                            id: result._id,
                            product: result.product,
                            quantity: result.quantity
                        }
                    });
                })
                .catch(err => {
                    return res.status(500).json({
                        success: false,
                        error: err.message
                    });
                });
        })
};


exports.order_get_by_id = (req, res, next) => {
    const id = req.params.id;

    Order.findById(id)
        .select("_id product quantity")
        .populate("product", "name")
        .then(order => {
            if (!order) {
                return res.status(404).json({
                    success: false,
                    message: "Order not found"
                });
            } else {
                return res.status(200).json({
                    success: true,
                    order: {
                        id: order._id,
                        quantity: order.quantity,
                        product: {
                            id: order.product._id,
                            name: order.product.name
                        }
                    }
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                message: err.message
            });
        });
};


exports.order_delete = (req, res, next) => {
    console.log(req.params.id);
    Order.findOneAndRemove({
            _id: req.params.id
        })
        .then(result => {
            if (result) {
                return res.status(200).json({
                    success: true,
                    message: "Order deleted"
                });
            } else {
                return res.status(400).json({
                    success: false,
                    error: 'Order 不存在'
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                error: err.message
            });
        });
};